//
//  WYNewListController.m
//  WYNews
//
//  Created by 曾佳豪 on 2017/3/18.
//  Copyright © 2017年 haodada.com. All rights reserved.
//

#import "WYNewListController.h"
#import <Masonry.h>
#import "WYNetworkTool.h"
#import "WYNetworkTool+News.h"
#import "WYNewsNormalCell.h"
#import "WYNewsHeadCell.h"
#import "WYBigImageCell.h"
#import "WYNewsMutiImageCell.h"
#import "WYNewsDetailController.h"
@interface WYNewListController ()<UITableViewDelegate ,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArrList;
@end

static NSString *identifier =@"identifier";
//headcell
static NSString *headNewsListCell =@"headNewsListCell";

//多张图片cell
static NSString *mutiImageCell =@"mutiImageCell";
//大图cell
static NSString *BigImageCell =@"BigImageCell";
@implementation WYNewListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [self setupUI];
    //调试代码
//    self.view.backgroundColor =  [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
//    [self.view addSubview:label];
//    label.font = [UIFont boldSystemFontOfSize:35];
//    label.textColor = [UIColor blackColor];
//    label.textAlignment = NSTextAlignmentCenter;
//    
//    label.text = [NSString stringWithFormat:@"第%zd个list,name:%@", _index,_model.tname];
    [self loadNewsData];
    

}
-(void)setupUI{
    _tableView =[[UITableView alloc]init];
    
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo (self.view);
        make.right.left .equalTo (self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        
    }];
    //注册cell
    [_tableView registerClass:[WYNewsNormalCell class] forCellReuseIdentifier:identifier];
    
    [_tableView registerClass:[WYNewsHeadCell class] forCellReuseIdentifier:headNewsListCell];
    [_tableView registerClass:[WYNewsMutiImageCell class] forCellReuseIdentifier:mutiImageCell];
    [_tableView registerClass:[WYBigImageCell class] forCellReuseIdentifier:BigImageCell];
    //设置代理和数据源
    _tableView.dataSource =self;
    _tableView.delegate =self;
    //自适应行高(预估行高+行高)
    _tableView .estimatedRowHeight =200;
    _tableView.rowHeight=UITableViewAutomaticDimension;
    
    //设置滚动范围可以替代自动布局 但还是尽量用自动布局
//    _tableView.contentInset =UIEdgeInsetsMake(0, 0, 153, 0);
}
//加载数据
-(void)loadNewsData{
    //导入工具类 调用工具的分类方法
   [[WYNetworkTool sharedTool]requestNewList:_model.tid callBack:^(NSArray *modelArr) {
       NSLog(@"%@",modelArr);
       //modelArr 就可以直接作为数据源使用
       //将模型数组添加到可变数组
       [_dataArrList addObjectsFromArray:modelArr];
       //获取到数据之后要刷新tab
       [_tableView reloadData];
       
   }];
}


#pragma  mark 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArrList .count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WYNewsModel *model =_dataArrList[indexPath.row];
    
    //判断模型是否是第一条
    if (model.hasHead) {
        //headcell
        WYNewsHeadCell *cell =(WYNewsHeadCell *)[tableView dequeueReusableCellWithIdentifier:headNewsListCell  forIndexPath:indexPath];
        cell.model =model;
        return cell;
    }
  
    //判断是否是大图
    if (model.imgType) {
        WYBigImageCell *cell =[tableView dequeueReusableCellWithIdentifier:BigImageCell forIndexPath:indexPath];
        cell.model =model;
        return cell;
    }
    //判断存储的多张图片数组是否不为nil
    if (model.imgextra  !=nil) {
        WYNewsMutiImageCell *cell =[tableView dequeueReusableCellWithIdentifier:mutiImageCell forIndexPath:indexPath];
        cell.model=model;
        return cell;
    }
    
    //不是第一条就给正常的
    
        WYNewsNormalCell *cell =(WYNewsNormalCell *)[tableView dequeueReusableCellWithIdentifier:identifier  forIndexPath:indexPath];
        cell.model =model;
        return cell;
  
    
}
//实现点击cell 跳转到详细页控制器
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //先获取到点击的那个model
    WYNewsModel *model =_dataArrList [indexPath.row];
    //创建详细页控制器
    WYNewsDetailController *detail =[WYNewsDetailController new];
    detail.model =model;
    //隐藏详细页控制器的TabBar
    detail.hidesBottomBarWhenPushed =YES;
    //跳转到详细页控制器
    [self.navigationController pushViewController:detail animated:YES];
    
}

-(id)initWithIndex:(NSInteger)index channelModel:(WYChannelModel *)model{
    if (self =[super init]) {
        //初始化index (就是将拿到的index 赋值给属性index)
        _index =index;
        //将初始化的model 赋值给属性model
        _model =model;
        _dataArrList =[NSMutableArray array];
    }
    return self;
}


@end
